﻿namespace Arms.Presentation.Web.Extensions
{
    using System;
    using System.Web.Mvc;
    using System.Collections.Generic;
    using System.Linq;

    public static class SelectListExtensions
    {
        public static SelectList ToSelectList<T>(this T source) where T : struct, IComparable, IFormattable, IConvertible
        {
            var values = from T e in Enum.GetValues(typeof(T))
                         select new { Id = e, Name = e.ToString() };

            return new SelectList(values, "Id", "Name", source);
        }

        public static SelectList FromEnum<T>(int selectedIndex)
        {
            var values = from T e in Enum.GetValues(typeof(T))
                         select new { Id = e, Name = e.ToString() };

            return new SelectList(values, "Id", "Name", selectedIndex);
        }
    }
}